/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.domain.das;

import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.domain.domain.UserRoleDomain;
import cn.hallele.ms.portalcenter.model.cache.UserRoleCache;
import cn.hallele.ms.portalcenter.model.entity.UserRoleEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/10/6 13:27
 */
@Slf4j
@Service
public class UserRoleDEDas extends ModelDas<UserRoleCache, UserRoleDomain, UserRoleEntity> {
    protected UserRoleDEDas(UserRoleCache option) {
        super(option);
    }

    /**
     * 通过用户id查询角色
     *
     * @param userId 用户id
     * @return 角色集合
     */
    public List<UserRoleDomain> findByUserId(Long userId) {
        return revertList(this.option.findByUserId(userId));
    }

    /**
     * 将T转成E
     *
     * @param userRoleDomain 被转换者
     * @return S 转换后者
     */
    @Override
    protected UserRoleEntity convert(UserRoleDomain userRoleDomain) {
        return ObjectUtil.map(userRoleDomain, u -> UserRoleEntity.builder()
                .id(u.getId())
                .userId(u.getUserId())
                .roleId(u.getRoleId())
                .createTime(u.getCreateTime())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param userRoleEntity 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected UserRoleDomain revert(UserRoleEntity userRoleEntity) {
        return ObjectUtil.map(userRoleEntity, u -> UserRoleDomain.builder()
                .id(u.getId())
                .userId(u.getUserId())
                .roleId(u.getRoleId())
                .createTime(u.getCreateTime())
                .build());
    }
}
